package fonkin.com.wifisignapp.retrofit;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import fonkin.com.wifisignapp.constants.AppConstants;
import fonkin.com.wifisignapp.retrofit.ServiceApis;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by 黄洪靖 on 2017/5/26.
 */

public class BaseRetrofit{

    public static ServiceApis GetBaseRetrofit(){
        HttpLoggingInterceptor logInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Log.d("HttpLog", message);
            }
        });
        logInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(15, TimeUnit.SECONDS)
                .addInterceptor(mInterceptor)//添加自定义拦截
                .addInterceptor(logInterceptor)//添加log拦截
                .build();



        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(AppConstants.COMMON_API)
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build();
        ServiceApis requestService = retrofit.create(ServiceApis.class);
        return requestService;
    }

    private static Interceptor mInterceptor = new Interceptor(){
        @Override
        public okhttp3.Response intercept(Chain chain) throws IOException {
            Request original = chain.request();
            Request.Builder requestBuilder = original.newBuilder()
                    .addHeader("uid","test")
                    .addHeader("username","hello");
            Request request = requestBuilder.build();
            return chain.proceed(request);
        }
    };




}
